<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


















  
  
    
  
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.css">







<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.1.1">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico?v=7.1.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico?v=7.1.1">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.1" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.1.1',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: true,
    fancybox: true,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="虽然之前写过一篇入门文章Git入门私房菜，但是工作中接触到的 Git 命令还有一些。有时候遇到一些小功能，就上网查一下，本文就针对用到的知识点做个全面的总结。">
<meta name="keywords" content="Git,Tools">
<meta property="og:type" content="article">
<meta property="og:title" content="Git使用教程笔记">
<meta property="og:url" content="https://michael728.github.io/2018/11/24/git-advance/index.html">
<meta property="og:site_name" content="Michael翔">
<meta property="og:description" content="虽然之前写过一篇入门文章Git入门私房菜，但是工作中接触到的 Git 命令还有一些。有时候遇到一些小功能，就上网查一下，本文就针对用到的知识点做个全面的总结。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://ws1.sinaimg.cn/mw690/6d9475f6ly1fxjh6nzepxj23vc2kwe81.jpg">
<meta property="og:image" content="https://michael728.github.io/2018/03/11/tools-zsh-tutorial/">
<meta property="og:updated_time" content="2020-01-05T15:47:56.116Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git使用教程笔记">
<meta name="twitter:description" content="虽然之前写过一篇入门文章Git入门私房菜，但是工作中接触到的 Git 命令还有一些。有时候遇到一些小功能，就上网查一下，本文就针对用到的知识点做个全面的总结。">
<meta name="twitter:image" content="https://ws1.sinaimg.cn/mw690/6d9475f6ly1fxjh6nzepxj23vc2kwe81.jpg">



  <link rel="alternate" href="/atom.xml" title="Michael翔" type="application/atom+xml">



  
  
  <link rel="canonical" href="https://michael728.github.io/2018/11/24/git-advance/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Git使用教程笔记 | Michael翔</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?1c9958c951d5a7661571422e0340e156";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <meta name="referrer" content="never">
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Michael翔</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">因上努力，果上随缘！</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tools">

    
    
    
      
    

    

    <a href="/tools/" rel="section"><i class="menu-item-icon fa fa-fw fa-location-arrow"></i> <br>利器</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-books">

    
    
    
      
    

    

    <a href="/books" rel="section"><i class="menu-item-icon fa fa-fw fa-book"></i> <br>阅读</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-movies">

    
    
    
      
    

    

    <a href="/movies" rel="section"><i class="menu-item-icon fa fa-fw fa-film"></i> <br>观影</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-think">

    
    
    
      
    

    

    <a href="/think" rel="section"><i class="menu-item-icon fa fa-fw fa-lightbulb-o"></i> <br>一言</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    
  
  

  

  <a href="https://github.com/michael728" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewbox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a>



    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://michael728.github.io/2018/11/24/git-advance/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Michael翔">
      <meta itemprop="description" content="DevOps CICD Blog 持续学习">
      <meta itemprop="image" content="/images/logo.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Michael翔">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Git使用教程笔记

              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-11-24 21:23:04" itemprop="dateCreated datePublished" datetime="2018-11-24T21:23:04+00:00">2018-11-24</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2020-01-05 15:47:56" itemprop="dateModified" datetime="2020-01-05T15:47:56+00:00">2020-01-05</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/ToolsDev/" itemprop="url" rel="index"><span itemprop="name">ToolsDev</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://ws1.sinaimg.cn/mw690/6d9475f6ly1fxjh6nzepxj23vc2kwe81.jpg" alt></p>
<p>虽然之前写过一篇入门文章<a href="https://michael728.github.io/2015/12/07/git-tutor/">Git入门私房菜</a>，但是工作中接触到的 Git 命令还有一些。有时候遇到一些小功能，就上网查一下，本文就针对用到的知识点做个全面的总结。</p>
<a id="more"></a>
<p>需要先在 Github 上创建个属于你的仓库，本文仓库名以 <code>michalel-git</code> 为例。</p>
<p>地址为： <a href="mailto:`git@github.com" target="_blank" rel="noopener">`git@github.com</a>:Michael728/michael-git.git`</p>
<h2 id="Git-安装"><a href="#Git-安装" class="headerlink" title="Git 安装"></a>Git 安装</h2><p><a href="https://git-scm.com/downloads" target="_blank" rel="noopener">Git下载地址</a></p>
<p>Windows安装时需要注意在<code>Configuring the line ending conversions</code>界面，选择<code>Checkout as-is,commit as -s</code>，避免Windows的换行符问题。如果忘记设置，可以使用如下命令后期设置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config --global core.autocrlf false</span><br></pre></td></tr></table></figure>
<p>参考：</p>
<ul>
<li><a href="https://github.com/cssmagic/blog/issues/22" target="_blank" rel="noopener">GitHub 第一坑：换行符自动转换 </a></li>
</ul>
<h2 id="Git-配置"><a href="#Git-配置" class="headerlink" title="Git 配置"></a>Git 配置</h2><p>可以通过 <code>git config -l</code> 查看配置。</p>
<h3 id="设置-Git-账号"><a href="#设置-Git-账号" class="headerlink" title="设置 Git 账号"></a>设置 Git 账号</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.name &quot;michael728&quot;</span><br><span class="line">git config --global user.email &quot;649168982@qq.com&quot;</span><br></pre></td></tr></table></figure>
<h3 id="Git-配置别名"><a href="#Git-配置别名" class="headerlink" title="Git 配置别名"></a>Git 配置别名</h3><p><code>git config</code>文件来轻松为每一个命令设置别名。例如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git config --global alias.co checkout</span><br><span class="line">git config --global alias.br branch</span><br><span class="line">git config --global alias.ci commit</span><br><span class="line">git config --global alias.st status</span><br></pre></td></tr></table></figure>
<p>如果你想要执行外包命令，而不是一个 Git 子命令，可以在命令前面加 <code>！</code> 符号。</p>
<p>演示将git visual定义为gitk的别名：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config --global alias.visual &apos;!gitk&apos;</span><br></pre></td></tr></table></figure>
<p>设置命令的别名，可以提高操作效率。查看<code>.gitconfig</code>文件<code>vim ~/.gitconfig</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">[user]</span><br><span class="line">	name = xxx</span><br><span class="line">	email = xxx</span><br><span class="line">[i18n]</span><br><span class="line">	commitencoding = utf-8</span><br><span class="line">	logoutputencoding = utf-8</span><br><span class="line">[core]</span><br><span class="line">	quotepath = false</span><br><span class="line">[filter &quot;lfs&quot;]</span><br><span class="line">	clean = git-lfs clean -- %f</span><br><span class="line">	smudge = git-lfs smudge -- %f</span><br><span class="line">	process = git-lfs filter-process</span><br><span class="line">	required = true</span><br><span class="line">[alias]</span><br><span class="line">	co = checkout</span><br><span class="line">	br = branch</span><br><span class="line">	c = commit</span><br><span class="line">	s = status</span><br><span class="line">	unstage = reset HEAD --</span><br><span class="line">	last = log -1 HEAD</span><br><span class="line">	lg = log --graph --pretty=format:&apos;%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&lt;%an&gt;%Creset&apos; --abbrev-commit --date=relative</span><br><span class="line"></span><br><span class="line">[color]</span><br><span class="line">	ui = true</span><br></pre></td></tr></table></figure>
<p>我们可以体验一个log的别名命令设置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lg = log --graph --pretty=format:&apos;%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&lt;%an&gt;%Creset&apos; --abbrev-commit --date=relative</span><br></pre></td></tr></table></figure>
<p>这是超厉害的别名缩写命令，试试现在的 <code>git lg</code> 有多酷炫吧！</p>
<h2 id="Git-帮助文档"><a href="#Git-帮助文档" class="headerlink" title="Git 帮助文档"></a>Git 帮助文档</h2><p>授人以鱼不如授人以渔，先知道怎么通过帮助文档查看常用命令的说明吧：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git help</span><br><span class="line">git help &lt;cmd&gt;</span><br></pre></td></tr></table></figure>
<h2 id="创建-Git-本地仓库"><a href="#创建-Git-本地仓库" class="headerlink" title="创建 Git 本地仓库"></a>创建 Git 本地仓库</h2><h3 id="已有远端仓库，创建本地仓库"><a href="#已有远端仓库，创建本地仓库" class="headerlink" title="已有远端仓库，创建本地仓库"></a>已有远端仓库，创建本地仓库</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git clone git@github.com:Michael728/michael-git.git</span><br><span class="line">cd michael-git</span><br><span class="line">touch README.md</span><br><span class="line">git add README.md</span><br><span class="line">git commit -m &quot;add README&quot;</span><br><span class="line">git push -u origin master</span><br></pre></td></tr></table></figure>
<h3 id="已存在文件夹"><a href="#已存在文件夹" class="headerlink" title="已存在文件夹"></a>已存在文件夹</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">cd micahel-git</span><br><span class="line">git init</span><br><span class="line">git remote add origin git@github.com:Michael728/michael-git.git</span><br><span class="line">git add .</span><br><span class="line">git commit</span><br><span class="line">git push -u origin master</span><br></pre></td></tr></table></figure>
<h3 id="已存在-Git-仓库"><a href="#已存在-Git-仓库" class="headerlink" title="已存在 Git 仓库"></a>已存在 Git 仓库</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cd existing_repo</span><br><span class="line">git remote add origin git@github.com:Michael728/michael-git.git</span><br><span class="line">git push -u origin --all # --all 表示 Push all branches，-u 选项指定了一个默认主机</span><br><span class="line">git push -u origin --tags # --tags All refs under refs/tags are pushed</span><br></pre></td></tr></table></figure>
<p>将本地的<code>master</code>分支推送到<code>origin</code>主机，同时指定<code>origin</code>为默认主机，后面就可以不加任何参数使用<code>git push</code>了。</p>
<p>参考：</p>
<ul>
<li><a href="https://www.zhihu.com/question/20019419" target="_blank" rel="noopener">git push 的 -u 参数具体适合含义？</a></li>
<li><a href="http://www.ruanyifeng.com/blog/2014/06/git_remote.html" target="_blank" rel="noopener">Git远程操作详解</a></li>
</ul>
<h2 id="git-clone"><a href="#git-clone" class="headerlink" title="git clone"></a>git clone</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone [-b br_name] url git@github.com:Michael728/michael-git.git</span><br></pre></td></tr></table></figure>
<p>克隆的时候，可以指定下载远端的分支、自定义本地仓库的名字。如果不加分支名参数，<code>git clone</code> 命令会默认自动设置本地 <code>master</code> 分支跟踪克隆的远程仓库的 <code>master</code> 分支（其实是仓库的默认分支）。而且，默认远程仓库设置别名为 <code>origin</code>。</p>
<h2 id="git-add"><a href="#git-add" class="headerlink" title="git add"></a>git add</h2><p>这是个多功能命令:</p>
<ul>
<li>可以用它开始跟踪新文件</li>
<li>把已跟踪的文件放到暂存区</li>
<li>还能用于合并时把有冲突的文件标记为已解决状态，这个是在解决冲突时会用到的功能</li>
</ul>
<h2 id="git-commit"><a href="#git-commit" class="headerlink" title="git commit"></a>git commit</h2><p>提交的操作，当你前面采用<code>add</code>命令将文件添加到暂存区跟踪后，需要通过<code>commit</code>将暂存区的内容提交到当前分支。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -m &quot;test&quot;</span><br></pre></td></tr></table></figure>
<p>当一些已追踪的文件修改了，常常需要<code>git add file</code>，然后在<code>git commit -m &quot;xxxx&quot;</code>，其实这两个步骤可以合二为一：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -am &quot;test&quot;</span><br></pre></td></tr></table></figure></p>
<p>这么写个人觉得挺好，可以有效避免有些懒人<code>git add .</code>的方式，将一切文件都添加到了暂存区，导致最后多余文件提交入库。</p>
<h2 id="git-push"><a href="#git-push" class="headerlink" title="git push"></a>git push</h2><p>语法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push &lt;远程主机名&gt; &lt;本地分支名&gt;:&lt;远程分支名&gt;</span><br></pre></td></tr></table></figure>
<p>如果省略远程分支名，则表示将本地分支推送与之存在”追踪关系”的远程分支（通常两者同名），如果该远程分支不存在，则会被新建。</p>
<p>当想要将<code>master</code>分支推送到<code>origin</code>服务器上：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin master</span><br></pre></td></tr></table></figure>
<p>将本地分支<code>test</code>推送到远端时可以重命名：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin test:remote-test</span><br></pre></td></tr></table></figure>
<p>利用该用法，还可以推送空分支到远端，实现远端分支的删除：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git push origin :remote-test</span><br><span class="line"># 和如下命令等同</span><br><span class="line">git push origin --delete remote-test</span><br></pre></td></tr></table></figure>
<h2 id="git-rm"><a href="#git-rm" class="headerlink" title="git rm"></a>git rm</h2><p>从 Git 中移除某个文件，就必须从已跟踪的文件清单中删除（从暂存区域移除文件），然后提交。可以使用<code>git rm</code>命令完成此项工作，并连带从工作目录中删除指定的文件。</p>
<p>当我们先把某文件从 Git 库中删除（亦即从暂存区移除），但仍然希望保留在当前工作目录中。比如当你忘记在<code>.gitignore</code>文件中将一些文件忽略，但是却不小心把大的日志文件添加到暂存区域时，这一做法很有用：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># --cached 将 README 文件从暂存区移除，但是工作区目录仍然保留</span><br><span class="line">git rm --cached README</span><br></pre></td></tr></table></figure>
<h2 id="git-checkout"><a href="#git-checkout" class="headerlink" title="git checkout"></a>git checkout</h2><p>切换到某个历史版本：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout &lt;commit id&gt;</span><br></pre></td></tr></table></figure>
<p>在 Git 中从当前分支创建并检出新分支的命令是：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b new-br</span><br></pre></td></tr></table></figure>
<p>这个命令实际是：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b new-br current-br</span><br></pre></td></tr></table></figure>
<p>在本地创建并切换到远端的分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git branch -va # 查看本地+远程分支列表</span><br><span class="line">git checkout -b dev  origin/dev</span><br></pre></td></tr></table></figure>
<p>还可以可以在<code>checkout</code>命令中使用Hash值作为起点创建分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b name-of-branch &lt;commit id&gt;</span><br></pre></td></tr></table></figure>
<p>除了有“切换”的意思，<code>checkout</code>还有一个撤销的作用。</p>
<p>举个例子，假设我们在一个分支开发一个小功能，刚写完一半，这时候需求变了，而且是大变化，之前写的代码完全用不了，好在你刚写，甚至都没有 <code>git add</code> 进暂存区，这个时候很简单的一个操作就直接把原文件还原：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout &lt;filename&gt;</span><br></pre></td></tr></table></figure>
<p>参考：<a href="https://www.cnblogs.com/crazyacking/p/5620635.html" target="_blank" rel="noopener">在git中checkout历史版本</a></p>
<h2 id="git-log"><a href="#git-log" class="headerlink" title="git log"></a>git log</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git log --pretty=oneline # 检查提交日志，都在一行：&lt;commit id&gt; &lt;message&gt;</span><br></pre></td></tr></table></figure>
<p>查看某人的提交：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git log --author=michael</span><br></pre></td></tr></table></figure>
<p>一个常用的选项是<code>-p</code>，用来显示每次提交的内容差异，也可以加上<code>-2</code>或者<code>-n2</code>来仅显示最近两次提交：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git log -p -2</span><br><span class="line">git log -n1 --format=format:%h # 查看当前分支最新的 commit id 缩略值</span><br></pre></td></tr></table></figure>
<p>列出最近两周内的提交：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git log --since=2.weeks</span><br></pre></td></tr></table></figure>
<ul>
<li><a href="http://www.cnblogs.com/bellkosmos/p/5923439.html" target="_blank" rel="noopener">git log命令全解析，打log还能这么随心所欲！</a></li>
<li><a href="https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%9F%A5%E7%9C%8B%E6%8F%90%E4%BA%A4%E5%8E%86%E5%8F%B2" target="_blank" rel="noopener">git-scm 2.3 Git 基础 - 查看提交历史</a></li>
</ul>
<h2 id="git-diff"><a href="#git-diff" class="headerlink" title="git diff"></a>git diff</h2><p><code>git diff</code>本身只显示尚未暂存的改动，而不是自上次提交以来所做的所有改动。</p>
<p>若要查看已暂存的将要添加到下次提交里的内容和上次提交的内容的变化，可以用<code>git diff --staged</code></p>
<h2 id="git-branch"><a href="#git-branch" class="headerlink" title="git branch"></a>git branch</h2><h3 id="新建develop分支："><a href="#新建develop分支：" class="headerlink" title="新建develop分支："></a>新建develop分支：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch develop</span><br></pre></td></tr></table></figure>
<h3 id="切换分支："><a href="#切换分支：" class="headerlink" title="切换分支："></a>切换分支：</h3><p>复习一下<code>checkout</code>的用法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout develop</span><br></pre></td></tr></table></figure>
<h3 id="新建并切换到develop分支："><a href="#新建并切换到develop分支：" class="headerlink" title="新建并切换到develop分支："></a>新建并切换到develop分支：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b develop</span><br></pre></td></tr></table></figure>
<h3 id="将分支develop推送到远程仓库origin："><a href="#将分支develop推送到远程仓库origin：" class="headerlink" title="将分支develop推送到远程仓库origin："></a>将分支develop推送到远程仓库<code>origin</code>：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin develop</span><br></pre></td></tr></table></figure>
<h3 id="如果想给远程的分支取名为develop2，可以："><a href="#如果想给远程的分支取名为develop2，可以：" class="headerlink" title="如果想给远程的分支取名为develop2，可以："></a>如果想给远程的分支取名为develop2，可以：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin develop:develop2</span><br></pre></td></tr></table></figure>
<p><strong>不建议这么操作易混乱，还是本地分支名和远程分支名保持一致比较好。</strong></p>
<h3 id="关联本地分支和远程分支："><a href="#关联本地分支和远程分支：" class="headerlink" title="关联本地分支和远程分支："></a>关联本地分支和远程分支：</h3><p>创建本地分支并切换到分支：<code>git checkout -b tools-dev</code></p>
<p>创建远程分支：<code>git push origin tools-dev</code></p>
<p>本地分支推送到远程服务器时，远程分支自动创建，推送本地分支到远程：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push --set-upstream &lt;remote-name&gt; &lt;local-branch-name&gt;:&lt;remote-branch-name&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li><code>&lt;remote-name&gt;</code>：远程 Git 服务器名称，一般为<code>origin</code></li>
<li><code>&lt;local-branch-name&gt;</code>：本地分支名称</li>
<li><code>&lt;remote-branch-name&gt;</code>：远程分支名称</li>
</ul>
<p>一般情况下，本地分支和远程分支名称相同，所以可简化为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push --set-upstream &lt;remote-name&gt; &lt;branch-name&gt;</span><br></pre></td></tr></table></figure>
<p><code>--set-upstream</code>参数用来关联本地分支和远程分支</p>
<p>参考：</p>
<ul>
<li><a href="https://majing.io/questions/718" target="_blank" rel="noopener">Git创建远程分支</a></li>
<li><a href="http://www.ruanyifeng.com/blog/2014/06/git_remote.html" target="_blank" rel="noopener">Git远程操作详解</a></li>
</ul>
<h3 id="查看本地分支："><a href="#查看本地分支：" class="headerlink" title="查看本地分支："></a>查看本地分支：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch</span><br></pre></td></tr></table></figure>
<h3 id="查看远程分支："><a href="#查看远程分支：" class="headerlink" title="查看远程分支："></a>查看远程分支：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch -r</span><br></pre></td></tr></table></figure>
<h3 id="删除本地分支："><a href="#删除本地分支：" class="headerlink" title="删除本地分支："></a>删除本地分支：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git branch -d develop</span><br><span class="line">git branch -D develop（强制删除）</span><br></pre></td></tr></table></figure>
<h3 id="删除远程分支："><a href="#删除远程分支：" class="headerlink" title="删除远程分支："></a>删除远程分支：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git push origin :remote-test</span><br><span class="line"># 和如下命令等同</span><br><span class="line">git push origin --delete remote-test</span><br></pre></td></tr></table></figure>
<h2 id="撤销操作"><a href="#撤销操作" class="headerlink" title="撤销操作"></a>撤销操作</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit --amend</span><br></pre></td></tr></table></figure>
<p>如果已经提交完了（已经<code>commit</code>）了，发现漏掉几个文件没有添加，或者提交信息（<code>-m</code>)写错了，可以运行带有<code>--amend</code>选项的提交命令重新提交。</p>
<p>这个命令会将<strong>暂存区</strong>的文件提交。文本编辑器编辑后，会覆盖原来的提交信息。</p>
<h3 id="取消暂存文件"><a href="#取消暂存文件" class="headerlink" title="取消暂存文件"></a>取消暂存文件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reset HEAD  &lt;file&gt;</span><br></pre></td></tr></table></figure>
<h3 id="撤销对文件的修改"><a href="#撤销对文件的修改" class="headerlink" title="撤销对文件的修改"></a>撤销对文件的修改</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -- &lt;file&gt;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>你需要知道 <code>git checkout -- [file]</code> 是一个危险的命令，这很重要.</p>
</blockquote>
<h2 id="远程仓库的使用"><a href="#远程仓库的使用" class="headerlink" title="远程仓库的使用"></a>远程仓库的使用</h2><h3 id="查看远程仓库"><a href="#查看远程仓库" class="headerlink" title="查看远程仓库"></a>查看远程仓库</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote -v</span><br></pre></td></tr></table></figure>
<p>如果想查看远程仓库更多的信息，可以使用<code>git remote show &lt;remote-name&gt;</code>命令。</p>
<p>远程仓库的移除与重命名</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git remote rename  &lt;old-remote-name&gt; &lt;new-remote-name&gt;</span><br><span class="line">git remote rename pb paul</span><br></pre></td></tr></table></figure>
<p>如果因为一些原因要移除一个远程仓库，可以使用<code>git remote rm &lt;remote-name&gt;</code>。</p>
<h3 id="添加一个新的远程-Git-仓库，同时指定一个可以轻松引用的简写："><a href="#添加一个新的远程-Git-仓库，同时指定一个可以轻松引用的简写：" class="headerlink" title="添加一个新的远程 Git 仓库，同时指定一个可以轻松引用的简写："></a>添加一个新的远程 Git 仓库，同时指定一个可以轻松引用的简写：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add &lt;remote-name&gt; &lt;url&gt;</span><br></pre></td></tr></table></figure>
<p>这里的<code>remote-name</code>常常取名为<code>origin</code>。所以，常见的<code>origin</code>其实是一个你 Git 仓库跟踪的远程仓库的简写。</p>
<h3 id="拉取远端仓库有但你本地没有的信息："><a href="#拉取远端仓库有但你本地没有的信息：" class="headerlink" title="拉取远端仓库有但你本地没有的信息："></a>拉取远端仓库有但你本地没有的信息：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git fetch &lt;remote-name&gt;</span><br></pre></td></tr></table></figure>
<p>如果你使用<code>clone</code>命令克隆了一个仓库，命令会自动将其添加为远程仓库并默认以<code>origin</code>为缩写。</p>
<h2 id="Tag"><a href="#Tag" class="headerlink" title="Tag"></a>Tag</h2><h3 id="列出标签"><a href="#列出标签" class="headerlink" title="列出标签"></a>列出标签</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git tag # 列出所有标签</span><br><span class="line">git tag -l &apos;v1.8*&apos; # 列出以 v1.8 开头的所有标签</span><br></pre></td></tr></table></figure>
<h3 id="创建标签"><a href="#创建标签" class="headerlink" title="创建标签"></a>创建标签</h3><p>Git使用两种主要类型的标签：</p>
<ul>
<li>附注（annotated）标签</li>
<li>轻量（ightweight）标签</li>
</ul>
<p>前者会包括一些注释信息，来进一步解释这个 tag 的作用，而后者就仅仅只是一个 tag 的名字</p>
<h4 id="附注标签"><a href="#附注标签" class="headerlink" title="附注标签"></a>附注标签</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git tag -a v1.4 -m &apos;my version 1.4&apos;</span><br></pre></td></tr></table></figure>
<p>通过<code>git show &lt;tag-name&gt;</code>命令可以看到标签信息</p>
<h4 id="轻量标签"><a href="#轻量标签" class="headerlink" title="轻量标签"></a>轻量标签</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git tag v1.4</span><br></pre></td></tr></table></figure>
<p>没用<code>-a</code>、<code>-m</code>的参数，只需要提供标签名字</p>
<h3 id="删除标签"><a href="#删除标签" class="headerlink" title="删除标签"></a>删除标签</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git tag -d &lt;tagname&gt;</span><br></pre></td></tr></table></figure>
<h3 id="补打标签"><a href="#补打标签" class="headerlink" title="补打标签"></a>补打标签</h3><h4 id="假设忘记给项目打标签，可以在之后加上："><a href="#假设忘记给项目打标签，可以在之后加上：" class="headerlink" title="假设忘记给项目打标签，可以在之后加上："></a>假设忘记给项目打标签，可以在之后加上：</h4><p>基于某历史节点的<code>commit id</code>补打<code>Tag</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git tag -a v1.2 &lt;commit id&gt;</span><br></pre></td></tr></table></figure>
<h4 id="共享标签"><a href="#共享标签" class="headerlink" title="共享标签"></a>共享标签</h4><p>默认情况下，<code>git push</code>命令并不会传送标签到远程服务器上。在创建完标签后你必须显示地推送标签到共享服务器上。这个过程就像共享远程分支一样，可以运行<code>git push origin [tagname]</code></p>
<p>如果想要一次性推送很多标签，也可以使用<code>--tags</code>选项的<code>git push</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin --tags</span><br></pre></td></tr></table></figure>
<h4 id="检出标签"><a href="#检出标签" class="headerlink" title="检出标签"></a>检出标签</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b &lt;new-br&gt; &lt;tagname&gt;</span><br></pre></td></tr></table></figure>
<p>参考：</p>
<ul>
<li><a href="https://thoamsy.github.io/blogs/git/" target="_blank" rel="noopener">The Junior Git</a></li>
<li><a href="https://git-scm.com/book/zh/v1/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE" target="_blank" rel="noopener">6 Git 基础 - 打标签</a></li>
</ul>
<h2 id="使用场景"><a href="#使用场景" class="headerlink" title="使用场景"></a>使用场景</h2><h3 id="新特性开发，创建临时分支，再合并到主干"><a href="#新特性开发，创建临时分支，再合并到主干" class="headerlink" title="新特性开发，创建临时分支，再合并到主干"></a>新特性开发，创建临时分支，再合并到主干</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">#创建特性分支</span><br><span class="line">git checkout -b featureA</span><br><span class="line">……</span><br><span class="line">#提交之前，先checkout到master分支，更新一下</span><br><span class="line">git co master</span><br><span class="line">git pull</span><br><span class="line">#切到特性分支，在本地与最新的master分支合并</span><br><span class="line">git co featureA</span><br><span class="line">git rebase -i master</span><br></pre></td></tr></table></figure>
<p>参考：</p>
<ul>
<li><a href="https://www.cnblogs.com/wangiqngpei557/p/5989292.html" target="_blank" rel="noopener">聊下git rebase -i</a></li>
</ul>
<h3 id="本地仓库关联远程仓库："><a href="#本地仓库关联远程仓库：" class="headerlink" title="本地仓库关联远程仓库："></a>本地仓库关联远程仓库：</h3><p>已有本地仓库，需要关联远端仓库，分两步：<br>第一步，在Github上新建一个仓库test；<br>第二步：将本地仓库与Github上的test项目进行关联，切换到本地仓库目录：<br><code>git remote add origin git@github.com:xx/test.git</code><br>什么意思？远程仓库的地址为：<a href="mailto:`git@github.com" target="_blank" rel="noopener">`git@github.com</a>:xx/test.git`，而origin是给这项目的远程仓库起的名字，是的，名字你可以随便取，只不过大家公认的只有一个远程仓库时名字就是origin，为什么要给远程仓库取名字？因为我们可能一个项目有多个远程仓库，比如，Github一个，比如公司一个，这样的话，提交的时候可以提交到不同的远程仓库就需要指定不同的仓库名字了。</p>
<p>查看我们当前项目有哪些远程仓库可以执行如下命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote -v</span><br></pre></td></tr></table></figure>
<p>接下来，本地的仓库可以向远程仓库进行代码提交了：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin master</span><br></pre></td></tr></table></figure>
<p>在代码提交之前，西安设置自己的用户名和邮箱，这些信息会出现在所有的commit记录里：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.name &quot;xxx&quot;</span><br><span class="line">git config --global user.email &quot;xx@mail.com&quot;</span><br></pre></td></tr></table></figure>
<h3 id="二分查找，傻瓜式定位bug"><a href="#二分查找，傻瓜式定位bug" class="headerlink" title="二分查找，傻瓜式定位bug"></a>二分查找，傻瓜式定位bug</h3><p>场景：定位Bug，当前版本有Bug，上个版本没有，两个版本之前有上千次commit<br>二分查找，N个patch只需要测试log2N次（8k个path仅需测试13次）<br>可以实现测试自动化，自动查找问题patch<br><code>git bisect</code>：自动定位，不必找原作者</p>
<h3 id="同时负责多个Bug的修改"><a href="#同时负责多个Bug的修改" class="headerlink" title="同时负责多个Bug的修改"></a>同时负责多个Bug的修改</h3><ul>
<li>更新远程仓库代码</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git fetch</span><br></pre></td></tr></table></figure>
<ul>
<li>以origin/master为基础创建分支</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b fix/bug23 origin/master</span><br></pre></td></tr></table></figure>
<ul>
<li>修改完</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit</span><br></pre></td></tr></table></figure>
<ul>
<li>推送前更新一下代码，看看别人是否有修改</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git fetch</span><br></pre></td></tr></table></figure>
<ul>
<li>有修改的话rebase一下</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rebase origin/master</span><br></pre></td></tr></table></figure>
<ul>
<li>生成patch</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git format-patch origin/master</span><br></pre></td></tr></table></figure>
<ul>
<li>发送patch或者也可以使用request pull</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">send email OR Request pull</span><br><span class="line">[maintainer接收后]</span><br></pre></td></tr></table></figure>
<ul>
<li>分支使用已经完成，可以删除了</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch -D fix/bug23</span><br></pre></td></tr></table></figure>
<h3 id="想知道某行代码谁修改的"><a href="#想知道某行代码谁修改的" class="headerlink" title="想知道某行代码谁修改的"></a>想知道某行代码谁修改的</h3><p>阅读代码时，想知道某行代码是谁修改的？</p>
<ul>
<li>找到对应commit id</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git blame src/xxx.c</span><br></pre></td></tr></table></figure>
<ul>
<li>查看具体提交的内容</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git show &lt;commit id&gt;</span><br></pre></td></tr></table></figure>
<h3 id="远端仓库会退到历史版本"><a href="#远端仓库会退到历史版本" class="headerlink" title="远端仓库会退到历史版本"></a>远端仓库会退到历史版本</h3><ul>
<li>查找commit id<br>通过<code>git log</code>查找想要会退到的历史版本的<code>commit id</code></li>
<li>本地执行回退</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reset --hard [commit id]</span><br></pre></td></tr></table></figure>
<ul>
<li>强制推送</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push -f</span><br></pre></td></tr></table></figure>
<p>参考：<a href="https://www.cnblogs.com/chenfulin5/p/6732036.html" target="_blank" rel="noopener">git 远程仓库版本的回退以及git reset 几种常用方式记录</a></p>
<h3 id="突然插入bugifx，回退工作目录"><a href="#突然插入bugifx，回退工作目录" class="headerlink" title="突然插入bugifx，回退工作目录"></a>突然插入bugifx，回退工作目录</h3><p><code>git stash</code>保存所有工作内容</p>
<h3 id="cherry-pick"><a href="#cherry-pick" class="headerlink" title="cherry-pick"></a>cherry-pick</h3><p>将某一提交点的修改拿到当前分支上：<br><code>git cherry-pick 哈希值</code></p>
<h2 id="基本的团队协作流程"><a href="#基本的团队协作流程" class="headerlink" title="基本的团队协作流程"></a>基本的团队协作流程</h2><p>多人协作下的分支管理规范很重要，就跟代码规范一样重要。</p>
<p>以下就跟大家推荐一种我们内部在使用的一种分支管理流程 Git Flow。</p>
<p>Gti Flow<br>Git Flow 是一种比较成熟的分支管理流程，我们先看一张图能清晰的描述他整个的工作流程：</p>
<p>大部分情况下都会拥有两个分支 master 和 develop，他们的职责分别是:</p>
<ul>
<li>master：永远处在即将发布(production-ready)状态</li>
<li>develop：最新的开发状态</li>
</ul>
<p>确切的说 master、develop 分支大部分情况下都会保持一致，只有在上线前的测试阶段<br>develop 比 master 的代码要多，一旦测试没问题，准备发布了，这时候会将 develop 合并到<br>master 上。</p>
<p>但是，我们发布之后又会进行下一版本的功能开发，开发中间可能又会遇到需要紧急修复 bug<br>，一个功能开发完成之后突然需求变动了等情况，所以 Git Flow 除了以上 master 和 develop<br>两个主要分支以外，还提出了以下三个辅助分支：</p>
<ul>
<li>feature: 开发新功能的分支, 基于 develop, 完成后 merge 回 develop</li>
<li>release: 准备要发布版本的分支, 用来修复 bug，基于 develop，完成后 merge 回develop 和 master</li>
<li>hotfix: 修复 master 上的问题, 等不及 release 版本就必须马上上线. 基于 master, 完成后merge 回 master 和 develop</li>
</ul>
<p>什么意思呢？<br>举个例子，假设我们已经有 master 和 develop 两个分支了，这个时候我们准备做一个功能A，第一步我们要做的，就是基于 develop 分支新建个分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch feature/A</span><br></pre></td></tr></table></figure>
<p>看到了吧，其实就是一个规范，规定了所有开发的功能分支都以 feature 为前缀。但是这个时候做着做着发现线上有一个紧急的 bug 需要修复，那赶紧停下手头的工作，立刻切换到 master 分支，然后再此基础上新建一个分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch hotfix/B</span><br></pre></td></tr></table></figure>
<p>代表新建了一个紧急修复分支，修复完成之后直接合并到 develop 和 master ，然后发布。然后再切回我们的 feature/A 分支继续着我们的开发，如果开发完了，那么合并回 develop 分支，然后在 develop 分支属于测试环境，跟后端对接并且测试的差不多了，感觉可以发布到正式环境了，这个时候再新建一个 release 分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch release/1.0</span><br></pre></td></tr></table></figure>
<p>这个时候所有的 api、数据等都是正式环境，然后在这个分支上进行最后的测试，发现 bug 直接进行修改，直到测试 ok 达到了发布的标准，最后把该分支合并到 develop 和 master 然后进行发布。</p>
<p>以上就是 Git Flow 的概念与大概流程，看起来很复杂，但是对于人数比较多的团队协作现实开发中确实会遇到这么复杂的情况，是目前很流行的一套分支管理流程，但是有人会问每次都要各种操作，合并来合并去，有点麻烦，哈哈，这点 Git Flow 早就想到了，为此还专门推出了一个 Git Flow 的工具，并且是开源的：</p>
<p>GitHub 开源地址： <a href="https://github.com/nvie/gitflow" target="_blank" rel="noopener">https://github.com/nvie/gitflow</a></p>
<p>简单点来说，就是这个工具帮我们省下了很多步骤，比如我们当前处于 master 分支，如果想要开发一个新的功能，第一步切换到 develop 分支，第二步新建一个以 feature 开头的分支名，有了 Git Flow 直接如下操作完成了：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git flow feature start A</span><br></pre></td></tr></table></figure>
<p>这个分支完成之后，需要合并到 develop 分支，然而直接进行如下操作就行：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git flow feature finish A</span><br></pre></td></tr></table></figure>
<p>如果是 hotfix 或者 release 分支甚至会自动帮你合并到 develop、master 两个分支。</p>
<p>想必大家已经了解了这个工具的具体作用，具体安装与用法我就不多提了，感兴趣的可以看<br>我下我之前写过的一篇博客： <a href="http://stormzhang.com/git/2014/01/29/git-flow/" target="_blank" rel="noopener">http://stormzhang.com/git/2014/01/29/git-flow/</a></p>
<h3 id="详细演示下怎么给一个项目发起-Pull-Request-PR-："><a href="#详细演示下怎么给一个项目发起-Pull-Request-PR-：" class="headerlink" title="详细演示下怎么给一个项目发起 Pull Request(PR)："></a>详细演示下怎么给一个项目发起 Pull Request(PR)：</h3><ul>
<li>第一步，找到你想发起 PR 的项目，点击右上角的 Fork 按钮，然后该项目就出现在了你自己账号的 Repository 里。</li>
<li>第二步，把fork的项目 clone 到本地，然后修改的 bug 也好，想要新增的功能也好，总之把自己做的代码改动开发完，接着，把自己做的代码改动 push 到 你自己的 GitHub 上去。</li>
<li>第三步，点击你 Fork 过来的项目主页的 Pull requests 页面，点击右上角的New pull request。<br>页面自动会比较该项目与原有项目的不同之处，最顶部声明了是源仓库的分支与你fork过来的分支的对比。同样的我写好标题和描述，然后我们点击中间的 Create pull request 按钮，至此我们就成功给该项目提交了一个 PR。<br>然后就等着项目原作者 review 你的代码，并且决定会不会接受你的 PR，如果接受，那么恭喜你，你已经是该项目的贡献者之一了。</li>
</ul>
<h2 id="Git-FAQ"><a href="#Git-FAQ" class="headerlink" title="Git FAQ"></a>Git FAQ</h2><h3 id="git-merge和git-rebase的区别"><a href="#git-merge和git-rebase的区别" class="headerlink" title="git merge和git rebase的区别"></a>git merge和git rebase的区别</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git checkout master</span><br><span class="line">git merge featureA</span><br></pre></td></tr></table></figure>
<p>其实 rebase 命令也是合并的意思，上面的需求我们一样可以如下操作：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git checkout master</span><br><span class="line">git rebase featureA</span><br></pre></td></tr></table></figure>
<p>rebase 跟 merge 的区别你们可以理解成有两个书架，你需要把两个书架的书整理到一起去，第一种做法是 merge ，比较粗鲁暴力，就直接腾出一块地方把另一个书架的书全部放进去，虽然暴力，但是这种做法你可以知道哪些书是来自另一个书架的；第二种做法就是rebase ，他会把两个书架的书先进行比较，按照购书的时间来给他重新排序，然后重新放置好，这样做的好处就是合并之后的书架看起来很有逻辑，但是你很难清晰的知道哪些书来自哪个书架的。</p>
<p>只能说各有好处的，不同的团队根据不同的需要以及不同的习惯来选择就好。</p>
<p>rebase 和 merge的另一个区别是rebase 的冲突是一个一个解决，如果有十个冲突，先解决第一个，然后用命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git add -u</span><br><span class="line">git rebase --continue</span><br></pre></td></tr></table></figure>
<p>继续后才会出现第二个冲突，直到所有冲突解决完，而merge 是所有的冲突都会显示出来。<br>另外如果rebase过程中，你想中途退出，恢复rebase前的代码则可以用命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rebase --abort</span><br></pre></td></tr></table></figure>
<p>关于<code>git rebase</code>还有很多知识点：</p>
<ul>
<li><a href="https://www.cnblogs.com/wangiqngpei557/p/5989292.html" target="_blank" rel="noopener">聊下git rebase -i</a></li>
<li><a href="https://blog.csdn.net/wh_19910525/article/details/7554489" target="_blank" rel="noopener">git merge 和 git rebase 小结</a></li>
<li><a href="http://gitbook.liuhui998.com/4_2.html" target="_blank" rel="noopener">Git Community Book 中文版-rebase</a></li>
<li><a href="https://www.cnblogs.com/wangdaijun/p/5956129.html" target="_blank" rel="noopener">压缩多个Commit</a></li>
<li><a href="https://www.jianshu.com/p/964de879904a" target="_blank" rel="noopener">合并多个 Commit</a></li>
</ul>
<h3 id="git-branch-r与git-branch-a的区别？"><a href="#git-branch-r与git-branch-a的区别？" class="headerlink" title="git branch -r与git branch -a的区别？"></a>git branch -r与git branch -a的区别？</h3><ul>
<li><code>git branch -r</code>只显示远端分支，</li>
<li><code>git branch -a</code> 显示本地分支和远程分支</li>
</ul>
<h2 id="发现好用的开源项目-Github"><a href="#发现好用的开源项目-Github" class="headerlink" title="发现好用的开源项目-Github"></a>发现好用的开源项目-Github</h2><p>GitHub 其中一个最重要的作用就是发现全世界最优秀的开源项目，你没事的时候刷刷微博、知乎，人家没事的时候刷刷 GitHub ，看看最近有哪些流行的项目，久而久之，这差距就越来越大，那么如何发现优秀的开源项目呢？</p>
<ol>
<li>关注一些活跃的大牛</li>
<li>Explore菜单下的Trending，看到最近的一些热门开源项目，很多人主动获取开源项目的最好的途径，可以选择“当天热门”，“一周之内热门”和“一月之内热门”来查看，并且，可以分语言来查看。</li>
<li>Search，按照Most Stars来筛选。<br>  除此之外，GitHub 的 Search 还有一些小技巧，比如你想搜索的结果中 star 数大于1000的，那么可以这样搜索：<pre><code>android http stars:&gt;1000
</code></pre></li>
</ol>
<p>有些人如果习惯用 Google 进行搜索，那么想搜索 GitHub 上的结果，不妨前面加 GitHub 关键字就ok了，比如我在 google 里输入 GitHub android http ，每个关键字用空格隔开。</p>
<h2 id="福利大放送"><a href="#福利大放送" class="headerlink" title="福利大放送"></a>福利大放送</h2><p>GitHub 上影响力很大，同时又对你们很有用的项目：</p>
<ul>
<li><a href="https://github.com/EbookFoundation/free-programming-books" target="_blank" rel="noopener">free-programming-books</a>:  这个项目整理了所有跟编程相关的免费书籍，而且全球多国语言版的都有，中文版的在这里 <a href="https://github.com/EbookFoundation/free-programming-books/blob/master/free-programm" target="_blank" rel="noopener">free-programming-books-zh</a></li>
<li><a href="https://github.com/robbyrussell/oh-my-zsh" target="_blank" rel="noopener">ob-my-zsh</a>:  俗话说，不会用 shell 的程序员不是真正的程序员。oh-my-zsh 毫无疑问就是目前最流行，最酷炫的 shell<img src="https://michael728.github.io/2018/03/11/tools-zsh-tutorial/" alt="zsh+on-my-zsh配置教程指南（程序员必备）"></li>
<li><a href="https://github.com/sindresorhus/awesome" target="_blank" rel="noopener">awesome</a>:  GitHub 上有各种 awesome 系列，简单来说就是这个系列搜罗整理了 GitHub 上各领域的资源大汇总，比如有 awesome-android, awesome-ios, awesome-java, awesome-python 等等等，就不截图了，你们自行去感受。</li>
<li><a href="https://github.com/tiimgreen/github-cheat-sheet/" target="_blank" rel="noopener">github-cheat-sheet</a>: GitHub 的使用有各种技巧，只不过基本的就够我们用了，但是如果你对 GitHub 超级感兴趣，想更多的了解 GitHub 的使用技巧，那么这个项目就刚好是你需要的，每个 GitHub 粉都应该知道这个项目。</li>
<li><a href="https://github.com/francistao/LearningNotes" target="_blank" rel="noopener">LearningNotes</a>：这是一份非常详细的面试资料，涉及 Android、Java、设计模式、算法等等等，你能想到的，你不能想到的基本都包含了，可以说是适应于任何准备面试的 Android 开发者，看完这个之后别说你还不知道怎么面试！</li>
</ul>
<p>GitHub 上优秀开源项目真的是一大堆，就不一一推荐了，授人以鱼不如授人以渔，请大家自行主动发掘自己需要的开源项目吧，不管是应用在实际项目上，还是对源码的学习，都是提升自己工作效率与技能的很重要的一个渠道，总有一天，你会突然意识到，原来不知不觉你已经走了这么远！</p>
<h2 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h2><ul>
<li><a href="https://michael728.github.io/2018/11/24/git-advance/">Git使用教程笔记</a></li>
<li><a href="https://git-scm.com/book/zh/v2" target="_blank" rel="noopener">Pro Git 中文</a></li>
<li><a href="https://foofish.net/git-command.html" target="_blank" rel="noopener">Git常用命令备忘</a></li>
</ul>

      
    </div>

    
      

  <div class="popular-posts-header">相关文章</div>
  <ul class="popular-posts">
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/2015/12/07/git-tutor/" rel="bookmark">Git入门私房菜</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/2018/03/11/tools-zsh-tutorial/" rel="bookmark">zsh+on-my-zsh配置教程指南（程序员必备）</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/2019/06/01/tools-dev-program-flowchart/" rel="bookmark">程序流程图学习笔记</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/2019/11/14/git-install-config/" rel="bookmark">Git 基础 —— 安装 配置 别名 对象</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/2019/11/14/git-usage-examples/" rel="bookmark">Git 基础 —— 常见使用场景</a></div>
      
    </li>
  
  </ul>


    

    
    
    

    
      <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center;">
  <img id="wechat_subscriber_qcode" src="/images/wechat-qcode.jpg" alt="Michael翔 wechat" style="width: 200px; max-width: 100%;">
  <div>欢迎订阅 ヾﾉ≧∀≦)o</div>
</div>

    

    
      
    
    
      <div>
        <div id="reward-container">
  <div>我知道是不会有人点的，但万一有人想不开呢👇</div>
  <button id="reward-button" disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">

    
      
      
        
      
      <div style="display: inline-block">
        <img src="/images/wechatpay.jpg" alt="Michael翔 微信支付">
        <p>微信支付</p>
      </div>
    
      
      
        
      
      <div style="display: inline-block">
        <img src="/images/alipay.png" alt="Michael翔 支付宝">
        <p>支付宝</p>
      </div>
    

  </div>
</div>

      </div>
    

    
      <div>
        




  



<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Michael翔</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    
    <a href="https://michael728.github.io/2018/11/24/git-advance/" title="Git使用教程笔记">https://michael728.github.io/2018/11/24/git-advance/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！</li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Git/" rel="tag"># Git</a>
          
            <a href="/tags/Tools/" rel="tag"># Tools</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/11/18/note-linux-basic-system/" rel="next" title="【笔记】快速上手 Linux，玩转典型应用">
                <i class="fa fa-chevron-left"></i> 【笔记】快速上手 Linux，玩转典型应用
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/12/02/tools-vim-plugin-config/" rel="prev" title="Vim 插件及配置">
                Vim 插件及配置 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8yODgyMC81Mzkw" "></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/logo.jpg" alt="Michael翔">
            
              <p class="site-author-name" itemprop="name">Michael翔</p>
              <div class="site-description motion-element" itemprop="description">DevOps CICD Blog 持续学习</div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">82</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">19</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">95</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/Michael728" title="GitHub &rarr; https://github.com/Michael728" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://weibo.com/1838446070/profile?topnav=1&wvr=6" title="微博 &rarr; https://weibo.com/1838446070/profile?topnav=1&wvr=6" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>微博</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://www.cnblogs.com/michael-xiang/" title="博客园 &rarr; https://www.cnblogs.com/michael-xiang/" rel="noopener" target="_blank"><i class="fa fa-fw fa-globe"></i>博客园</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://www.zhihu.com/people/michaelXoX" title="知乎 &rarr; https://www.zhihu.com/people/michaelXoX" rel="noopener" target="_blank"><i class="fa fa-fw fa-globe"></i>知乎</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://twitter.com/728_michael" title="Twitter &rarr; https://twitter.com/728_michael" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://t.me/michaelxiang" title="Telegram &rarr; https://t.me/michaelxiang" rel="noopener" target="_blank"><i class="fa fa-fw fa-telegram"></i>Telegram</a>
                </span>
              
            </div>
          

          
             <div class="cc-license motion-element" itemprop="license">
              
              
                
              
              
              
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
             </div>
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                关注列表
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://michael728.github.io/2018/09/16/blog-interesting/" title="https://michael728.github.io/2018/09/16/blog-interesting/">Blog List</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://zcheng.ren/posts/" title="https://zcheng.ren/posts/" rel="noopener" target="_blank">ZeeCoder</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://ehlxr.me/" title="https://ehlxr.me/" rel="noopener" target="_blank">ehlxr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.kawabangga.com/" title="https://www.kawabangga.com/" rel="noopener" target="_blank">卡瓦邦噶-蚂蚁SRE</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.stdioa.com/" title="https://blog.stdioa.com/" rel="noopener" target="_blank">David Dai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://bestzuo.cn/" title="https://bestzuo.cn/" rel="noopener" target="_blank">Sanarous</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.zhyong.cn/" title="https://www.zhyong.cn/" rel="noopener" target="_blank">ZhangYong-爱折腾软件</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.dalaoyang.cn/" title="https://www.dalaoyang.cn/" rel="noopener" target="_blank">Dalaoyang-SpringBoot</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.wuliaole.com/index.html" title="http://www.wuliaole.com/index.html" rel="noopener" target="_blank">无聊小博</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ilovey.live/" title="http://ilovey.live/" rel="noopener" target="_blank">低调小熊猫-Java开发</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://michael728.github.io/2016/09/03/blog-logfile/" title="https://michael728.github.io/2016/09/03/blog-logfile/">建站日志</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Git-安装"><span class="nav-number">1.</span> <span class="nav-text">Git 安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Git-配置"><span class="nav-number">2.</span> <span class="nav-text">Git 配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#设置-Git-账号"><span class="nav-number">2.1.</span> <span class="nav-text">设置 Git 账号</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Git-配置别名"><span class="nav-number">2.2.</span> <span class="nav-text">Git 配置别名</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Git-帮助文档"><span class="nav-number">3.</span> <span class="nav-text">Git 帮助文档</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建-Git-本地仓库"><span class="nav-number">4.</span> <span class="nav-text">创建 Git 本地仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#已有远端仓库，创建本地仓库"><span class="nav-number">4.1.</span> <span class="nav-text">已有远端仓库，创建本地仓库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#已存在文件夹"><span class="nav-number">4.2.</span> <span class="nav-text">已存在文件夹</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#已存在-Git-仓库"><span class="nav-number">4.3.</span> <span class="nav-text">已存在 Git 仓库</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-clone"><span class="nav-number">5.</span> <span class="nav-text">git clone</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-add"><span class="nav-number">6.</span> <span class="nav-text">git add</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-commit"><span class="nav-number">7.</span> <span class="nav-text">git commit</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-push"><span class="nav-number">8.</span> <span class="nav-text">git push</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-rm"><span class="nav-number">9.</span> <span class="nav-text">git rm</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-checkout"><span class="nav-number">10.</span> <span class="nav-text">git checkout</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-log"><span class="nav-number">11.</span> <span class="nav-text">git log</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-diff"><span class="nav-number">12.</span> <span class="nav-text">git diff</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git-branch"><span class="nav-number">13.</span> <span class="nav-text">git branch</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#新建develop分支："><span class="nav-number">13.1.</span> <span class="nav-text">新建develop分支：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#切换分支："><span class="nav-number">13.2.</span> <span class="nav-text">切换分支：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#新建并切换到develop分支："><span class="nav-number">13.3.</span> <span class="nav-text">新建并切换到develop分支：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#将分支develop推送到远程仓库origin："><span class="nav-number">13.4.</span> <span class="nav-text">将分支develop推送到远程仓库origin：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如果想给远程的分支取名为develop2，可以："><span class="nav-number">13.5.</span> <span class="nav-text">如果想给远程的分支取名为develop2，可以：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关联本地分支和远程分支："><span class="nav-number">13.6.</span> <span class="nav-text">关联本地分支和远程分支：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看本地分支："><span class="nav-number">13.7.</span> <span class="nav-text">查看本地分支：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看远程分支："><span class="nav-number">13.8.</span> <span class="nav-text">查看远程分支：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除本地分支："><span class="nav-number">13.9.</span> <span class="nav-text">删除本地分支：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除远程分支："><span class="nav-number">13.10.</span> <span class="nav-text">删除远程分支：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#撤销操作"><span class="nav-number">14.</span> <span class="nav-text">撤销操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#取消暂存文件"><span class="nav-number">14.1.</span> <span class="nav-text">取消暂存文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#撤销对文件的修改"><span class="nav-number">14.2.</span> <span class="nav-text">撤销对文件的修改</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#远程仓库的使用"><span class="nav-number">15.</span> <span class="nav-text">远程仓库的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#查看远程仓库"><span class="nav-number">15.1.</span> <span class="nav-text">查看远程仓库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#添加一个新的远程-Git-仓库，同时指定一个可以轻松引用的简写："><span class="nav-number">15.2.</span> <span class="nav-text">添加一个新的远程 Git 仓库，同时指定一个可以轻松引用的简写：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#拉取远端仓库有但你本地没有的信息："><span class="nav-number">15.3.</span> <span class="nav-text">拉取远端仓库有但你本地没有的信息：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Tag"><span class="nav-number">16.</span> <span class="nav-text">Tag</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#列出标签"><span class="nav-number">16.1.</span> <span class="nav-text">列出标签</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建标签"><span class="nav-number">16.2.</span> <span class="nav-text">创建标签</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#附注标签"><span class="nav-number">16.2.1.</span> <span class="nav-text">附注标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#轻量标签"><span class="nav-number">16.2.2.</span> <span class="nav-text">轻量标签</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除标签"><span class="nav-number">16.3.</span> <span class="nav-text">删除标签</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#补打标签"><span class="nav-number">16.4.</span> <span class="nav-text">补打标签</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#假设忘记给项目打标签，可以在之后加上："><span class="nav-number">16.4.1.</span> <span class="nav-text">假设忘记给项目打标签，可以在之后加上：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#共享标签"><span class="nav-number">16.4.2.</span> <span class="nav-text">共享标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#检出标签"><span class="nav-number">16.4.3.</span> <span class="nav-text">检出标签</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用场景"><span class="nav-number">17.</span> <span class="nav-text">使用场景</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#新特性开发，创建临时分支，再合并到主干"><span class="nav-number">17.1.</span> <span class="nav-text">新特性开发，创建临时分支，再合并到主干</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#本地仓库关联远程仓库："><span class="nav-number">17.2.</span> <span class="nav-text">本地仓库关联远程仓库：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二分查找，傻瓜式定位bug"><span class="nav-number">17.3.</span> <span class="nav-text">二分查找，傻瓜式定位bug</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#同时负责多个Bug的修改"><span class="nav-number">17.4.</span> <span class="nav-text">同时负责多个Bug的修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#想知道某行代码谁修改的"><span class="nav-number">17.5.</span> <span class="nav-text">想知道某行代码谁修改的</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#远端仓库会退到历史版本"><span class="nav-number">17.6.</span> <span class="nav-text">远端仓库会退到历史版本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#突然插入bugifx，回退工作目录"><span class="nav-number">17.7.</span> <span class="nav-text">突然插入bugifx，回退工作目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cherry-pick"><span class="nav-number">17.8.</span> <span class="nav-text">cherry-pick</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基本的团队协作流程"><span class="nav-number">18.</span> <span class="nav-text">基本的团队协作流程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#详细演示下怎么给一个项目发起-Pull-Request-PR-："><span class="nav-number">18.1.</span> <span class="nav-text">详细演示下怎么给一个项目发起 Pull Request(PR)：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Git-FAQ"><span class="nav-number">19.</span> <span class="nav-text">Git FAQ</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#git-merge和git-rebase的区别"><span class="nav-number">19.1.</span> <span class="nav-text">git merge和git rebase的区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#git-branch-r与git-branch-a的区别？"><span class="nav-number">19.2.</span> <span class="nav-text">git branch -r与git branch -a的区别？</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#发现好用的开源项目-Github"><span class="nav-number">20.</span> <span class="nav-text">发现好用的开源项目-Github</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#福利大放送"><span class="nav-number">21.</span> <span class="nav-text">福利大放送</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#最后"><span class="nav-number">22.</span> <span class="nav-text">最后</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Michael翔</span>

  

  
</div>









        








        
      </div>
    </footer>

    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>












  



  
    
    
      
    
  
  <script color="0,0,255" opacity="0.5" zindex="-1" count="99" src="//cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-nest@1.0.0/canvas-nest.min.js"></script>













  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.js"></script>


  


  <script src="/js/utils.js?v=7.1.1"></script>

  <script src="/js/motion.js?v=7.1.1"></script>



  
  


  <script src="/js/affix.js?v=7.1.1"></script>

  <script src="/js/schemes/pisces.js?v=7.1.1"></script>




  
  <script src="/js/scrollspy.js?v=7.1.1"></script>
<script src="/js/post-details.js?v=7.1.1"></script>



  


  <script src="/js/next-boot.js?v=7.1.1"></script>


  

  

  

  


  
    <script>
  window.livereOptions = {
    refer: '2018/11/24/git-advance/'
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
</script>

  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
  

  
  

  


  

  

  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>


  

  

  

  

  

  
<script>
  $('.highlight').not('.gist .highlight').each(function(i, e) {
    var $wrap = $('<div>').addClass('highlight-wrap');
    $(e).after($wrap);
    $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function(e) {
      var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
        return $(e).text();
      }).toArray().join('\n');
      var ta = document.createElement('textarea');
      var yPosition = window.pageYOffset || document.documentElement.scrollTop;
      ta.style.top = yPosition + 'px'; // Prevent page scroll
      ta.style.position = 'absolute';
      ta.style.opacity = '0';
      ta.readOnly = true;
      ta.value = code;
      document.body.appendChild(ta);
      ta.select();
      ta.setSelectionRange(0, code.length);
      ta.readOnly = false;
      var result = document.execCommand('copy');
      
        if (result) $(this).text('复制成功');
        else $(this).text('复制失败');
      
      ta.blur(); // For iOS
      $(this).blur();
    })).on('mouseleave', function(e) {
      var $b = $(this).find('.copy-btn');
      setTimeout(function() {
        $b.text('复制');
      }, 300);
    }).append(e);
  })
</script>


  

  

</body>
</html>
